# EA SPF DATA RGDP Clean
rm(list=ls())
(WD <- getwd())
if (!is.null(WD)) setwd(WD)
indata      = paste0(WD,"/_easpf/","", collapse = NULL)

library(haven)
library(AER)
library(sandwich)
library(lmtest)
library(pracma)
library(stargazer)
library(plm)
library(pracma)
library(DataCombine)
library(jtools)
library(plyr)
library(readxl)
library(tidyr)

###### CLEAN GDP ######
for (ii in 1999:2020) {
  for (jj in 1:4){
    tmp   = paste(indata,paste0(paste0(paste0(ii,"Q"),jj),".csv"),sep="")
    data_tmp = read.csv(tmp, skip = 1)
    
    data_tmp$id       = data_tmp$FCT_SOURCE
    data_tmp$target   = data_tmp$TARGET_PERIOD
    data_tmp$forecast = data_tmp$POINT
    
    data_tmp = subset(data_tmp, select = c(id,target,forecast))
    
    ind               = grep("^GROWTH",data_tmp$target)
    ind1              = grep("^EXPECTED",data_tmp$target)
    ind               = ind+2
    ind1              = ind1-2
    data_gdp          = data_tmp[ind:ind1,]
    
    data_gdp          = spread(data_gdp, target, forecast)
    data_gdp          = data_gdp[c(1,3,5)]
    
    names(data_gdp)[2] = "forecast1"
    names(data_gdp)[3] = "forecast2"
    
    data_gdp$year    = ii
    data_gdp$quarter = jj
    
    data  = data_gdp
    tmp   = paste0(paste0(paste0(ii,"Q"),jj),".rda")
    save(data, file = tmp)
  }
}

rm(list=ls())
for (ii in 1999:2020) {
  for (jj in 1:4){
    tmp   = paste0(paste0(paste0(ii,"Q"),jj),".rda")
    load(tmp)
    
    data_tmp = data
    
    if (ii == 1999 && jj == 1 ) {
      data_final  = data_tmp
    } else {
      data_final = merge(data_final,data_tmp, all = "TRUE")
    }
    
  }
}

data              = data_final
data$forecast1    = as.numeric(levels(data$forecast1))[as.integer(data$forecast1)]
data$forecast2    = as.numeric(levels(data$forecast2))[as.integer(data$forecast2)]
data$id           = as.numeric(data$id)

data = data[order(data$year,data$quarter),]

data$quarter_h1[data$quarter == 1] = 3
data$quarter_h1[data$quarter == 2] = 4
data$quarter_h1[data$quarter == 3] = 1
data$quarter_h1[data$quarter == 4] = 2

data$year_h1                       = data$year + 1
data$year_h1[data$quarter == 1]    = data$year[data$quarter == 1]
data$year_h1[data$quarter == 2]    = data$year[data$quarter == 2]

data$quarter_h2  = data$quarter_h1
data$year_h2     = data$year_h1+1

data_f1          = subset(data, select = c(year_h1,quarter_h1,id,forecast1))
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,forecast1))

data_f2          = subset(data, select = c(year_h2,quarter_h2,id,forecast2))
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,forecast2))

data_clean       = merge(data_f1, data_f2, all = "TRUE")

data_gdp        = read_excel("_easpf/data_realizations.xlsx", sheet = "Export")
data_gdp        = subset(data_gdp, select = c(year,quarter, rgdp_yoy,fly) )

data_clean      = merge(data_clean, data_gdp)


###### SAVE THE DATA GDP ########
data            = data_clean
data$qdate      = data$year + data$quarter/4-0.25
data$fc_err     = data$rgdp_yoy-data$forecast1
data$fc_rev     = data$forecast1-data$forecast2 
data$f1         = data$forecast1
data$f2         = data$forecast2
data$y_realz    = data$rgdp_yoy
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,y_realz,fc_err,fc_rev))

data            = data[data$year>="2000",]

save(data,file="ea_spf_rgdp_ind.rda")
clear()
